<?php
use yii\db\Migration;
use common\helpers\Constants;

class m150912_123002_system_tables extends Migration
{

    public function safeUp()
    {
        $tableOptions = null;
        if ($this->db->driverName === 'mysql') {
            $tableOptions = 'CHARACTER SET utf8 COLLATE utf8_unicode_ci ENGINE=InnoDB';
        }
        
        $this->createTable('{{%data_dictionary_category}}', [
            'id' => $this->string(64)
                ->unique(),
            'name' => $this->string(64)
                ->notNull(),
            'description' => $this->string(512)
                ->notNull()
        ], $tableOptions);
        
        $this->addPrimaryKey('pk_data_dictionary_category', '{{%data_dictionary_category}}', 'id');
        
        $this->createTable('{{%data_dictionary}}', [
            'id' => $this->primaryKey(),
            'parent_id' => $this->integer()
                ->defaultValue(null),
            'category_id' => $this->string(64)
                ->notNull(),
            'name' => $this->string(64)
                ->notNull(),
            'value' => $this->text()
                ->notNull(),
            'description' => $this->string(512),
            'thumbnail' => $this->string(512),
            'sort' => $this->smallInteger(4),
            'status' => $this->smallInteger(4)
                ->notNull()
                ->defaultValue(Constants::STATUS_ENABLE)
        ], $tableOptions);
        
        $this->createTable('{{%menu_category}}', [
            'id' => $this->string(64)
                ->notNull(),
            'name' => $this->string(64)
                ->notNull(),
            'description' => $this->string(512)
                ->defaultValue(null)
        ], $tableOptions);
        
        $this->addPrimaryKey('pk_menu_category', '{{%menu_category}}', 'id');
        
        $this->createTable('{{%menu}}', [
            'id' => $this->primaryKey(),
            'parent_id' => $this->integer()
                ->defaultValue(null),
            'category_id' => $this->string(64)
                ->notNull(),
            'name' => $this->string(64)
                ->notNull(),
            'url' => $this->string(512)
                ->notNull(),
            'target' => $this->string(512),
            'description' => $this->string(512),
            'icon' => $this->string(),
            'status' => $this->smallInteger(4)
                ->defaultValue(Constants::STATUS_ENABLE),
            'sort' => $this->integer()
                ->defaultValue(0)
        ], $tableOptions);
        
        $this->addForeignKey('fk_data_dictionary_parent', '{{%data_dictionary}}', 'parent_id', '{{%data_dictionary}}', 'id', 'cascade', 'cascade');
        $this->addForeignKey('fk_data_dictionary_category', '{{%data_dictionary}}', 'category_id', '{{%data_dictionary_category}}', 'id', 'cascade', 'cascade');
        
        $this->addForeignKey('fk_menu_parent', '{{%menu}}', 'parent_id', '{{%menu}}', 'id', 'cascade', 'cascade');
        $this->addForeignKey('fk_menu_category', '{{%menu}}', 'category_id', '{{%menu_category}}', 'id', 'cascade', 'cascade');
    }

    public function safeDown()
    {
        $this->dropForeignKey('fk_menu_category', '{{%menu}}');
        $this->dropForeignKey('fk_menu_parent', '{{%menu}}');
        
        $this->dropForeignKey('fk_data_dictionary_category', '{{%data_dictionary}}');
        $this->dropForeignKey('fk_data_dictionary_parent', '{{%data_dictionary}}');
        
        $this->dropTable('{{%menu}}');
        $this->dropTable('{{%menu_category}}');
        
        $this->dropTable('{{%data_dictionary}}');
        $this->dropTable('{{%data_dictionary_category}}');
    }
}
